package com.example.shoppingmall.cotroller;

import com.example.shoppingmall.config.MyLog;
import com.example.shoppingmall.pojo.LogisticsCompany;
import com.example.shoppingmall.service.LogisticsCompanyService;
import org.springframework.core.ReactiveTypeDescriptor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/logisticsCompany")
public class LogisticsCompanyController {

    @Resource
    LogisticsCompanyService logisticsCompanyService;

    @RequestMapping("/getLogistics")
    public String getLogisticsCompany(
            @RequestParam(value = "dang", required = false) String dangIng,
            Model model) {

        int dang = 1;
        if (dangIng != null & dangIng != "") {
            dang = Integer.parseInt(dangIng);
        }
        int size = 6;
        int count = logisticsCompanyService.count();
        int zong = count % size == 0 ? count / size : count / size + 1;
        model.addAttribute("logistics", logisticsCompanyService.getAll(dang, size));
        model.addAttribute("dang", dang);
        model.addAttribute("zong", zong);
        return "/logistics_company/logistics_company";
    }


    @MyLog(value = "控制面板->物流公司->删除")
    @RequestMapping("/del")
    @ResponseBody
    public String del(int id) {
        String data="删除失败!";
        int num=logisticsCompanyService.del(id);
        if (num>0){
            data="删除成功!";
        }
        return data;
    }

    @MyLog(value = "控制面板->物流公司->显示修改详情")
    @RequestMapping("/getByIdLogisticsCompany")
    @ResponseBody
    public Map<Object,Object> getByIdLogisticsCompany( int id){
        Map<Object,Object> map=new HashMap<>();
        map.put("logisticsCompany",logisticsCompanyService.getById(id));
        return map;
    }


    @MyLog(value = "控制面板->物流公司->修改")
    @RequestMapping("/updateLogisticsCompany")
    public String updateLogisticsCompany(LogisticsCompany logisticsCompany){
        int num=logisticsCompanyService.updateLogisticsCompany(logisticsCompany);
        if (num>0){
            return "redirect:/logisticsCompany/getLogistics";
        }
        return "/logistics_company/logistics_company";
    }


    @MyLog(value = "控制面板->物流公司->添加")
    @RequestMapping("/addLogisticsCompany")
    public String addLogisticsCompany(LogisticsCompany logisticsCompany){
        int num=logisticsCompanyService.addLogisticsCompany(logisticsCompany);
        if (num>0){
            return "redirect:/logisticsCompany/getLogistics";
        }
        return "/logistics_company/logistics_company";
    }
}
